package com.inei.practicadecampo.tools;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.apache.http.protocol.HTTP;
import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.sax.StartElementListener;
import android.telephony.TelephonyManager;
import android.util.Xml;

import com.inei.practicadecampo.model.entity.Respuesta;

public class Exportar {

	Context context;

	public Exportar(Context context) {
		this.context = context;
	}

	public boolean xml(List<Respuesta> lista) {
		File file = null;
		File ruta = null;

		String imei = "";
		try {
			TelephonyManager tm = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			imei = tm.getDeviceId();
		} catch (Exception e) {
			imei = "";
		}
		try {
			File root = Environment.getExternalStorageDirectory();
			ruta = new File(root, "/Evaluacion de la Practica de Campo/");
			if (!ruta.exists()) {
				ruta.mkdirs();
			}
			file = new File(ruta, "E_Prac_Campo.xml");
			FileOutputStream fos = new FileOutputStream(file);
			XmlSerializer xml = Xml.newSerializer();
			xml.setOutput(fos, HTTP.UTF_8);
			xml.startDocument(HTTP.UTF_8, true);

			xml.startTag("", "script");
			for (int i = 0; i < lista.size(); i++) {
				Respuesta r = lista.get(i);
				xml.startTag("", "respuesta");

				if (imei != null) {
					xml.startTag("", "Tablet_id");
					xml.text(imei);
					xml.endTag("", "Tablet_id");
				}

				xml.startTag("", "formato_id");
				xml.text("" + 3);
				xml.endTag("", "formato_id");

				xml.startTag("", "etapa");
				xml.text("" + 2);
				xml.endTag("", "etapa");

				xml.startTag("", "ape_paterno");
				xml.text("" + r.getApe_paterno());
				xml.endTag("", "ape_paterno");

				xml.startTag("", "ape_materno");
				xml.text("" + r.getApe_materno());
				xml.endTag("", "ape_materno");

				xml.startTag("", "nombre");
				xml.text("" + r.getNombre());
				xml.endTag("", "nombre");

				xml.startTag("", "dni");
				xml.text("" + r.getDni());
				xml.endTag("", "dni");

				xml.startTag("", "ape_paterno2");
				xml.text("" + r.getApe_paterno2());
				xml.endTag("", "ape_paterno2");

				xml.startTag("", "ape_materno2");
				xml.text("" + r.getApe_materno2());
				xml.endTag("", "ape_materno2");

				xml.startTag("", "nombre2");
				xml.text("" + r.getNombre2());
				xml.endTag("", "nombre2");

				xml.startTag("", "dni2");
				xml.text("" + r.getDni2());
				xml.endTag("", "dni2");

				xml.startTag("", "P01");
				xml.text("" + r.getP01());
				xml.endTag("", "P01");

				xml.startTag("", "P02");
				xml.text("" + r.getP02());
				xml.endTag("", "P02");

				xml.startTag("", "P03");
				xml.text("" + r.getP03());
				xml.endTag("", "P03");

				xml.startTag("", "P04");
				xml.text("" + r.getP04());
				xml.endTag("", "P04");

				xml.startTag("", "P05");
				xml.text("" + r.getP05());
				xml.endTag("", "P05");

				xml.startTag("", "P06");
				xml.text("" + r.getP06());
				xml.endTag("", "P06");

				xml.startTag("", "P07");
				xml.text("" + r.getP07());
				xml.endTag("", "P07");

				xml.startTag("", "P08");
				xml.text("");
				xml.endTag("", "P08");

				xml.startTag("", "P09");
				xml.text("");
				xml.endTag("", "P09");

				xml.startTag("", "P10");
				xml.text("");
				xml.endTag("", "P10");

				xml.startTag("", "nota");
				xml.text("" + r.getNota());
				xml.endTag("", "nota");

				xml.endTag("", "respuesta");

			}
			xml.endTag("", "script");
			xml.endDocument();
			fos.close();

		} catch (Exception e) {
			return false;
		}
		return true;
	}

}
